/**
 * 
 */
package com.baijia.tianxiao.push.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.baijia.tianxiao.push.dao.mybatis.PushBindRecordMapper;
import com.baijia.tianxiao.push.enums.PushCodeEnum;
import com.baijia.tianxiao.push.exception.BizException;
import com.baijia.tianxiao.push.model.PushBindRecord;
import com.baijia.tianxiao.push.model.UserDeviceAttribute;
import com.baijia.tianxiao.push.service.PushBindRecordService;
import com.baijia.tianxiao.push.service.UserDeviceAttributeService;

/**
 * @author leiruiqi
 *
 */
@Service("pushBindRecordService")
public class PushBindRecordServiceImpl implements PushBindRecordService{

	private static Logger logger = LoggerFactory.getLogger(PushBindRecordServiceImpl.class);
	
	@Resource
	private PushBindRecordMapper pushBindRecordMapper;
	
	@Resource
	private UserDeviceAttributeService userDeviceAttributeService;

	@Override
	public void saveNewBind(PushBindRecord pushBindRecord) {
		checkValidataBindUser(pushBindRecord);
		
		pushBindRecord.buildChanels();
		
		pushBindRecordMapper.insert(pushBindRecord);
		
	}

	private void checkValidataBindUser(PushBindRecord pushBindRecord){
		if(StringUtils.isBlank(pushBindRecord.getUserId())
				||StringUtils.isBlank(pushBindRecord.getUserType())
				){			
				throw new BizException(PushCodeEnum.bind_invalid_parameter.name());
			}
		
		if(pushBindRecord.getBindTime() == null){
			pushBindRecord.setBindTime(new Date());
		}
		if(pushBindRecord.getBindStatus() == null){
			pushBindRecord.setBindStatus(1);
		}
	}
	
	
	@Override
	public List<PushBindRecord> qureyUserBindList(PushBindRecord pushBindRecord,Integer limit) {
		checkValidataBindUser(pushBindRecord);
		/*if(limit == null || limit<1){
			throw new BizException(PushCodeEnum.bind_invalid_parameter.name());
		}*/
		List<PushBindRecord> recordList = pushBindRecordMapper.qureyUserBindList(pushBindRecord.getUserId(), pushBindRecord.getUserType(),limit);
		
		//Map<String,UserDeviceAttribute> deviceAttributeMap = userDeviceAttributeService.queryMapByBizAndUserId(pushBindRecord.getUserType(), pushBindRecord.getUserId());
		for(PushBindRecord record:recordList){
			record.buildChannelMap();
			/*String deviceId = record.getDeviceId();
			if(StringUtils.isNotBlank(deviceId)){
				UserDeviceAttribute deviceAttribute = deviceAttributeMap.get(record.getDeviceId());
				if(deviceAttribute!=null){
					record.setDeviceAttribute(deviceAttribute.getAttribute());
				}
			}*/
			
		}
		
		return recordList;
	}
	
	

	@Override
	public List<PushBindRecord> qureyUserBindList(String userId, String userType, Integer limit) {
		PushBindRecord record = new PushBindRecord();
		record.setUserId(userId);
		record.setUserType(userType);
		return qureyUserBindList(record,limit);
	}

	@Override
	public PushBindRecord queryUserBindLatest(PushBindRecord pushBindRecord) {
		checkValidataBindUser(pushBindRecord);
			
		PushBindRecord recode = pushBindRecordMapper.queryUserBindLatest(pushBindRecord.getUserId(), pushBindRecord.getUserType());
		if(recode != null){
			recode.buildChannelMap();
		}
		return recode;
	}

	@Override
	public PushBindRecord queryUserBindLatest(String userId, String userType) {
		PushBindRecord record = new PushBindRecord();
		record.setUserId(userId);
		record.setUserType(userType);
		return queryUserBindLatest(record);
	}

	@Override
	public void unBindLatest(PushBindRecord pushBindRecord) {
		checkValidataBindUser(pushBindRecord);
		pushBindRecordMapper.updateLatestForUnbind(pushBindRecord.getUserId(), pushBindRecord.getUserType());
	}

	@Override
	public void unBindLatest(String userId, String userType) {
		PushBindRecord record = new PushBindRecord();
		record.setUserId(userId);
		record.setUserType(userType);
		unBindLatest(record);
	}

	@Override
	public List<PushBindRecord> qureyUserBindByPrefix(String userIdPrefix, String userType, Integer limit) {
		userIdPrefix = StringUtils.replace(userIdPrefix, "_", "\\_");
		List<PushBindRecord> bindRecordlist = pushBindRecordMapper.qureyUserBindListByPrefix(userIdPrefix, userType, limit);
		//Map<String,UserDeviceAttribute> deviceAttributeMap = userDeviceAttributeService.queryMapByBizAndUserIdPrefix(userType, userIdPrefix);
		for(PushBindRecord record:bindRecordlist){
			record.buildChannelMap();
			/*String deviceId = record.getDeviceId();
			if(StringUtils.isNotBlank(deviceId)){
				UserDeviceAttribute deviceAttribute = deviceAttributeMap.get(record.getDeviceId());
				if(deviceAttribute!=null){
					record.setDeviceAttribute(deviceAttribute.getAttribute());
				}
			}*/
			
		}
		return bindRecordlist;
	}

	@Override
	public List<PushBindRecord> qureyUserBind(String userId, String userType) {
		PushBindRecord record = new PushBindRecord();
		record.setUserId(userId);
		record.setUserType(userType);
		return qureyUserBindList(record,null);
	}

	@Override
	public void unBindByDeviceId(String deviceId) {
		/*List<PushBindRecord> list = pushBindRecordMapper.queryUserBindListByDeviceId(deviceId,null);
		
		for(PushBindRecord pushBindRecord:list){
			PushBindRecord tempRecord = new PushBindRecord();
			tempRecord.setId(pushBindRecord.getId());
			tempRecord.setBindStatus(0);
			pushBindRecordMapper.updateByPrimaryKeySelective(tempRecord);
		}*/
		pushBindRecordMapper.updateBindStatusByDeviceId(deviceId);
	}

	@Override
	public List<PushBindRecord> queryUserBindListByDeviceId(String deviceId) {
		List<PushBindRecord> recordList =  pushBindRecordMapper.queryUserBindListByDeviceId(deviceId,null);
		for(PushBindRecord recode:recordList){
			recode.buildChannelMap();
		}
		return recordList;
	}

	@Override
	public int saveOfUpdateForBind(PushBindRecord record) {
		checkValidataBindUser(record);
		record.buildChanels();
		PushBindRecord oldRecord = this.pushBindRecordMapper.getUserBindByDeviceId(record.getDeviceId());
		if(oldRecord !=null){
			return pushBindRecordMapper.updateBindInfoByDeviceId(record);
		}else{
			try {
				return pushBindRecordMapper.insert(record);
			} catch (Exception e) {
				return pushBindRecordMapper.updateBindInfoByDeviceId(record);
			}
		}
		
	}

}
